package main

func validPath(n int, edges [][]int, source int, destination int) bool {

	path := make(map[int][]int)
	for _, edge := range edges {
		_, ok := path[edge[0]]
		if ok {
			path[edge[0]] = append(path[edge[0]], edge[1])
		} else {
			path[edge[0]] = make([]int, 1)
			path[edge[0]][0] = edge[1]
		}
	}

	bfs := func(src, end int) int {
		queue := make([]int, 0)
		queue = append(queue, src)
		for len(queue) > 0 {
			size := len(queue)

			for i := 0; i < size; i++ {
				srcPath := queue[i]
				paths := path[srcPath]
				if srcPath == destination {
					return 1
				}
				queue = append(queue, paths...)
			}
			queue = queue[1:]
		}

		return 0
	}

	if bfs(source, destination) == 0 {
		return false
	}
	return true
}
